
package com.zhengtoon.mctoon.conv.utils;


import com.bjtoon.framework.sdk.message.im.IMContent;
import com.bjtoon.framework.sdk.message.im.IMEntity;
import com.bjtoon.framework.sdk.message.im.IMResult;
import com.bjtoon.framework.sdk.message.im.ToonIMService;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.msg.MessageAppRequest;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * Toon 通知 utils
 *
 * @author liuzhenhua
 * @version 2017-04-15
 *          Copyright: Copyright (c) 2017
 *          Company:北京政务通科技有限公司
 */
public class IMUtils {

    private static Logger log = LoggerFactory.getLogger(IMUtils.class);

    private final static Integer MSG_SEND_SUCCESS_CODE = 0;

    // 通知类型
    private final static String MSG_TYPE = "51";

    private static final Integer CATALOG_ID = 110;

    // 发消息http地址
    private static String SEND_MESSAGE_URL = SysConfig.getPropertyValue("config.im.send_message_url");

    // 通知设置
    private final static String APP_ID = SysConfig.getPropertyValue("config.appId");
    private final static String APP_SECRET = SysConfig.getPropertyValue("config.appSecret");


    /**
     * 发送通知消息
     *
     * @param messageAppRequest 通知记录实体
     * @return String
     * @author 144637 liuzhenhua
     */
    public static String sendToonMessage(MessageAppRequest messageAppRequest) {

        String userId = messageAppRequest.getUserId();
        String content = messageAppRequest.getMsgContent();
        verifyParam(userId, content);

        IMEntity entity = buildIMEntity(messageAppRequest);

        IMResult result = new ToonIMService().sendMessage(entity);

        // 成功返回空,失败返回消息参数
        if (MSG_SEND_SUCCESS_CODE.equals(result.getStatus())) {
            // 返回通知业务编号
            return entity.getBizNo();
        } else {
            return null;
        }
    }


    /**
     * 构建消息实体
     *
     * @param messageAppRequest 通知记录实体
     * @return String
     * @author 144637 liuzhenhua
     */
    private static IMEntity buildIMEntity(MessageAppRequest messageAppRequest) {

        IMEntity entity = IMEntity.builder();

        String bizNo = messageAppRequest.getBizNo();
        String userId = messageAppRequest.getUserId();
        String msgContent = messageAppRequest.getMsgContent();
        String msgUrl = messageAppRequest.getMsgUrl();
        String buttonTitle = messageAppRequest.getButtonTitle();
        Integer finishFlag = messageAppRequest.getFinishFlag();

        // 设置消息 bizNo
        entity.setBizNo(bizNo);
        entity.setAppId(APP_ID);
        entity.setFrom(APP_ID);
        entity.setAppSecret(APP_SECRET);
        entity.setSendIMUrl(SEND_MESSAGE_URL);
        entity.setTo("0");
        entity.setToClient(userId);
        entity.setMsgType(MSG_TYPE);
        entity.setCatalog(AppConstant.MSG_CATALOG_TEMPLATE);
        entity.setCatalogId(CATALOG_ID);
        entity.setSubCatalog(AppConstant.MSG_CATALOG_TEMPLATE);

        // 设置通知状态
        entity.setFinishFlag(finishFlag);

        if (StringUtils.isNotBlank(msgUrl)) {
            entity.setActionType(1);
            entity.setSummary(msgContent);
            entity.setPushInfo(AppConstant.MSG_SUMMARY_TEMPLATE);

            // 设置消息体内容
            IMContent content = new IMContent();
            content.setUrl(msgUrl);
            content.setButtonTitle(buttonTitle);
            entity.setIMContent(content);
        } else {
            entity.setActionType(0);
            entity.setSummary(msgContent);
            entity.setPushInfo(AppConstant.MSG_SUMMARY_TEMPLATE);
        }

        return entity;
    }


    /**
     * 校验参数
     *
     * @return content
     * @author 144637 liuzhenhua
     */
    private static void verifyParam(String userId, String content) {

        if (StringUtils.isAnyBlank(userId, content)) {
            throw new BusinessException(ConvPhotoExceptionCode.MSG_PARAM_IS_NULL);
        }
    }

}
